package com.yeswater.multithread.chapter1.test1_12.test2;

import org.junit.Test;

/**
 * author : yeswater
 * date : 2023/10/19
 *
 * 如果 不调用 t1.resume(),t1.suspend() 会一直占据 printString() 的锁
 */

public class RunTest {

    @Test
    public void test1() {
        try {
            final SynchronizedObject object = new SynchronizedObject();

            Thread t1 = new Thread(() -> object.printString());

            t1.setName("t1");
            t1.start();

            Thread.sleep(1000);

            Thread t2 = new Thread(() -> {
                System.out.println("t2 start, but not invoke printString(), just print 'begin' ");
                System.out.println("because of printString() is locked and suspended forever by t1 ");
                object.printString();
            });
            t2.start();

            // System.out.println("info ==== t1.resume");
            // t1.resume();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * t1.suspend() PrintStream 对象锁, "main end" 不能输出
     */
    @Test
    public void test2() {
        try {
            MyThread t1 = new MyThread();
            t1.start();
            Thread.sleep(1000);
            t1.suspend();
            System.out.println("main end!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
